package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ethink.order.api.info.TocSaleImportLogDetailInfo;
import com.ethink.order.api.request.TocSaleImportLogDetailAddRequest;
import com.ethink.order.api.request.TocSaleImportLogDetailQueryRequest;
import com.ethink.order.common.entity.TocSaleImportLogDetail;
import com.ethink.order.common.mapper.TocSaleImportLogDetailMapper;
import com.ethink.order.converter.TocSaleImportLogDetailConverter;
import com.ethink.order.service.TocSaleImportLogDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * TOC销售单导入日志明细 Service接口实现
 *
 * @author default
 * @date 2022-11-22
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TocSaleImportLogDetailServiceImpl implements TocSaleImportLogDetailService {

    @Autowired
    private TocSaleImportLogDetailMapper tocSaleImportLogDetailMapper;

    @Override
    public void batchInsert(List<TocSaleImportLogDetailAddRequest> item, Long id, Map<String, BigDecimal> rateMap) {
        for (TocSaleImportLogDetailAddRequest tocSaleImportLogDetailAddRequest : item) {
            BigDecimal bigDecimal = rateMap.get(DateUtil.date(tocSaleImportLogDetailAddRequest.getOrderTime()).toString("yyyy-MM-dd"));
            if (bigDecimal == null) {
                bigDecimal = rateMap.get(DateUtil.date().toString("yyyy-MM-dd"));
            }
            insert(tocSaleImportLogDetailAddRequest, id, bigDecimal);
        }
    }

    @Override
    public Integer insert(TocSaleImportLogDetailAddRequest request, Long logId, BigDecimal exchangeRate) {
        TocSaleImportLogDetail tocSaleImportLogDetail = BeanUtil.copyProperties(request, TocSaleImportLogDetail.class);
        tocSaleImportLogDetail.setLogId(logId);
        tocSaleImportLogDetail.setSalePrice(request.getSalePriceWb().multiply(exchangeRate));
        return tocSaleImportLogDetailMapper.insert(tocSaleImportLogDetail);
    }

    @Override
    public List<TocSaleImportLogDetailInfo> queryDetailByShopOrderNoAndMateSku(List<TocSaleImportLogDetailQueryRequest> requests) {
        if (CollUtil.isEmpty(requests)) {
            return new ArrayList<>();
        }
        return tocSaleImportLogDetailMapper.queryDetailByShopOrderNoAndMateSku(requests);
    }

    @Override
    public List<TocSaleImportLogDetailInfo> queryDetailByShopOrderNo(List<String> shopOrderNoList) {
        if (CollUtil.isEmpty(shopOrderNoList)) {
            return CollectionUtil.newArrayList();
        }
        List<String> list = shopOrderNoList.stream().distinct().collect(Collectors.toList());
        return tocSaleImportLogDetailMapper.queryDetailByShopOrderNo(list);
    }

    @Override
    public List<TocSaleImportLogDetailInfo> queryDetailByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return CollUtil.newArrayList();
        }
        return tocSaleImportLogDetailMapper.queryDetailByIds(ids);
    }
}
